//
//  HomeViewCell.swift
//  sina_yl
//
//  Created by 袁量 on 2017/5/23.
//  Copyright © 2017年 袁量. All rights reserved.
//

import UIKit
import SDWebImage
private let margin : CGFloat = 15
private let ItemMargin : CGFloat = 10
class HomeViewCell: UITableViewCell {

    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var verifiedView: UIImageView!
    @IBOutlet weak var screenNameLabel: UILabel!
    @IBOutlet weak var vipView: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var retweetedLabel: UILabel!
    @IBOutlet weak var retweetedBG: UIView!
    
//    @IBOutlet weak var collectionView: PicCollectionView!
    @IBOutlet weak var homeLableWidth: NSLayoutConstraint!
    @IBOutlet weak var picView: PicCollectionView!
    @IBOutlet weak var collectionViewH: NSLayoutConstraint!
    @IBOutlet weak var collectionViewW: NSLayoutConstraint!
//    @IBOutlet weak var picView: PicCollectionView!
//    
//    @IBOutlet weak var collectionViewW: NSLayoutConstraint!
//    @IBOutlet weak var collectionViewH: NSLayoutConstraint!
    
    var viewModel : StatusViewModel? {
        didSet{
            guard let viewModel = viewModel else {
                return
            }
            iconView.sd_setImage(with: viewModel.profileURL as URL?, placeholderImage: UIImage(named: "avatar_default_small"))
            iconView.layer.cornerRadius = iconView.bounds.size.width / 2
            iconView.layer.masksToBounds = true
            verifiedView.image = viewModel.verifiedImage
            screenNameLabel.text = viewModel.status?.user?.screen_name
            vipView.image = viewModel.vipImage
            timeLabel.text = viewModel.createAtText
            sourceLabel.text = viewModel.sourceText
            screenNameLabel.textColor = viewModel.vipImage == nil ? UIColor.black : UIColor.orange
            contentLabel.text = viewModel.status?.text
            
            let picViewSize = calculatePicViewSize(numOfcount: viewModel.picURLs.count)
            collectionViewH.constant = picViewSize.height
            collectionViewW.constant = picViewSize.width
            picView.picURLs = viewModel.picURLs as [URL]
            
            if viewModel.status?.retweeted_status != nil {
                if let screenName = viewModel.status?.user?.screen_name, let retweetedText = viewModel.status?.retweeted_status?.text {
                    retweetedLabel.text = "@" + "\(screenName) :" + retweetedText
                }
                retweetedBG.isHidden = false
            } else {
                retweetedLabel.text = nil
                retweetedBG.isHidden = true
            }
        }
    }
    

    override func awakeFromNib() {
        super.awakeFromNib()
        homeLableWidth.constant = UIScreen.main.bounds.width - 2 * margin

//        let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
//        let imageViewWH = (UIScreen.main.bounds.width - 2 * margin - 2 * ItemMargin) / 3
//        layout.itemSize = CGSize(width: imageViewWH, height: imageViewWH)
//        layout.minimumLineSpacing = 0.1
//        layout.minimumInteritemSpacing = 0.1
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
// MARK: - 计算方法 
extension HomeViewCell {
    func calculatePicViewSize(numOfcount : Int) -> CGSize {
        // 1.没有配图
        if numOfcount == 0 {
            return CGSize.zero
        }
        
        // 2.取出picView对应的layout
        let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
        
        
        
        
        // 3.单张配图
        if numOfcount == 1 {
            // 1.取出图片
            let urlString = viewModel?.picURLs.last?.absoluteString
//            print("一张图片时候是 \(urlString)")
            let image = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: urlString!)
            // 2.设置一张图片是layout的itemSize
            print("照片找到了")
            layout.itemSize = CGSize(width: (image?.size.width)! * 2, height: (image?.size.height)! * 2)
            
            return CGSize(width: image!.size.width * 2, height: image!.size.height * 2)
        }
        
        // 4.计算出来imageViewWH
        let imageViewWH = (UIScreen.main.bounds.width - 2 * margin - 2 * ItemMargin) / 3
        
        // 5.设置其他张图片时layout的itemSize
        layout.itemSize = CGSize(width: imageViewWH, height: imageViewWH)
        
        // 6.四张配图
        if numOfcount == 4 {
            let picViewWH = imageViewWH * 2 + ItemMargin
            return CGSize(width: picViewWH, height: picViewWH)
        }
        
        // 7.其他张配图
        // 7.1.计算行数
        let rows = CGFloat((numOfcount - 1) / 3 + 1)
        
        // 7.2.计算picView的高度
        let picViewH = rows * imageViewWH + (rows - 1) * ItemMargin
        
        // 7.3.计算picView的宽度
        let picViewW = UIScreen.main.bounds.width - 2 * margin
        
        return CGSize(width: picViewW, height: picViewH)
        
        
        }
}

